import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.realm.text.IniRealm;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.Factory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

// 官方 QuickStart 示例
public class QuickStart {

	private static final transient Logger log = LoggerFactory.getLogger(QuickStart.class);


	public static void main(String[] args) {
		// 被弃用的方法
//		Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini");
//		SecurityManager securityManager = factory.getInstance();

		// 新的方法
		DefaultSecurityManager securityManager = new DefaultSecurityManager();
		IniRealm iniRealm = new IniRealm("classpath:shiro.ini");
		securityManager.setRealm(iniRealm);

		SecurityUtils.setSecurityManager(securityManager);

		// 获取当前用户对象 Subject
		Subject currentUser = SecurityUtils.getSubject();

		// 通过当前用户 获取 Session
		Session session = currentUser.getSession();
		session.setAttribute("someKey", "aValue");
		log.info("Subject.session 中 设置 someKey => aValue");

		String value = (String) session.getAttribute("someKey");
		if (value.equals("aValue")) {
			log.info("Subject.session 中 获取 someKey => [" + value + "]");
		}

		// 判断当前用户是否已认证
		if (!currentUser.isAuthenticated()) {
			// 用户令牌 Token
			UsernamePasswordToken token = new UsernamePasswordToken("lonestarr", "vespa");
			// 设置 记住登录状态
			token.setRememberMe(true);
			try {
				currentUser.login(token);
			} catch (UnknownAccountException uae) {
				log.info("There is no user with username of " + token.getPrincipal());
			} catch (IncorrectCredentialsException ice) {
				log.info("Password for account " + token.getPrincipal() + " was incorrect!");
			} catch (LockedAccountException lae) {
				log.info("The account for username " + token.getPrincipal() + " is locked.  " +
						"Please contact your administrator to unlock it.");
			}
			// ... catch more exceptions here (maybe custom ones specific to your application?
			catch (AuthenticationException ae) {
				//unexpected condition?  error?
			}
		}

		//say who they are:
		//print their identifying principal (in this case, a username):
		log.info("User [" + currentUser.getPrincipal() + "] logged in successfully.");

		//test a role:
		if (currentUser.hasRole("schwartz")) {
			log.info("May the Schwartz be with you!");
		} else {
			log.info("Hello, mere mortal.");
		}

		// 粗粒度 检查权限
		if (currentUser.isPermitted("lightsaber:wield")) {
			log.info("You may use a lightsaber ring.  Use it wisely.");
		} else {
			log.info("Sorry, lightsaber rings are for schwartz masters only.");
		}

		// 细粒度 检查权限
		if (currentUser.isPermitted("winnebago:drive:eagle5")) {
			log.info("You are permitted to 'drive' the winnebago with license plate (id) 'eagle5'.  " +
					"Here are the keys - have fun!");
		} else {
			log.info("Sorry, you aren't allowed to drive the 'eagle5' winnebago!");
		}

		// 注销
		currentUser.logout();

		// 结束
		System.exit(0);
	}
}